package com.t114514.demo.controller;

import com.t114514.demo.pojo.Message;
import com.t114514.demo.pojo.PageBean;
import com.t114514.demo.pojo.Result;
import com.t114514.demo.pojo.Teacher;
import com.t114514.demo.service.MessageService;
import com.t114514.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private TeacherService teacherService;

    // 学生给老师留言
    @PostMapping("/addMessage")
    public Result addMessage(@RequestBody Message message) {
        Teacher teacher=teacherService.findTeacher(message.getTeacherId());
        message.setTeacherName(teacher.getTeacherName());
        messageService.addMessage(message);
        return Result.success("留言成功");
    }

    // 老师查看留言
    @GetMapping("/getMessages")
    public Result<PageBean<Message>> getMessages(@RequestParam(required = false) int teacherId,

                                         @RequestParam int pageNum,
                                         @RequestParam int pageSize) {
        PageBean<Message> messagePageBean = messageService.findMessage(teacherId,pageNum,pageSize);
        return Result.success(messagePageBean);
    }
    @GetMapping("/getAllMessages")
    public Result<PageBean<Message>> getAllMessages(@RequestParam(required = false) String teacherName,
                                                 @RequestParam int pageNum,
                                                 @RequestParam int pageSize) {
        PageBean<Message> messagePageBean = messageService.findMessage(teacherName, pageNum, pageSize);
        return Result.success(messagePageBean);
    }
    @DeleteMapping("/deleteMessage")
    public Result deleteMessage(@RequestParam int messageId) {
        messageService.removeMessage(messageId);
        return Result.success("删除成功");
    }
}